const app = getApp();
import util from '../../../utils/util'
import {
  GetOrder,
  WxPayTrade
} from '../../../config/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 0, //物品数量
    price: 0, //物品价格
    img: '', //商品图片
    name: '', //商品名称
    id: 0, //商品id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log('物品价格与件数：', options);
    this.setData({
      num: options.num,
      price: options.price,
      img: options.img,
      name: options.name,
      id: options.id,
    });

  },

  /**
   * 获取表单的值
   */
  formSubmit: function(e) {
    let that = this;
    let openid = wx.getStorageSync('openid');
    if (e.detail.value.name == '' || e.detail.value.address == '' || e.detail.value.link == '') {
      console.log('您的信息没有填充完整！');
      wx.showModal({
        title: '提示',
        content: '您的信息没有填充完整！',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            return;
          }
        }
      });


    }


    let params = {
      'id': that.data.id, //商品id
      'openid': openid,
      'u_name': e.detail.value.name, //购买人姓名
      'u_addr': e.detail.value.address, //收货地址
      'u_phone': e.detail.value.link, //联系电话
      'num': that.data.num, //购买数量
      vollege_id: app.globalData.vollege_id
    };
    console.log('params,', params);
    util.apiRequest(WxPayTrade, params).then(r => {
      console.log('订单信息', r);
      wx.showToast({
        icon:"none",
        title: r.data.info,
        duration: 2000
      })
      let trade_no = r.data.data.trade_no;
      wx.requestPayment({
        timeStamp: r.data.data.timeStamp,
        nonceStr: r.data.data.nonceStr,
        package: r.data.data.package,
        signType: 'MD5',
        paySign: r.data.data.paySign,
        success(res) {
          console.log("success", res)
          //return false;
          util.apiRequest(GetOrder, {
            'trade_no': trade_no
          }, null).then(r => {
            console.log("支付完成查询订单支付结果", r.info)
            wx.showToast({
              title: r.info,
              duration: 2000
            })
          })
          console.log("success", res)
        },
        fail(res) {
          console.log("fail", res)
        }
      })
    });
  },

  /**
   * 获取输入框输入的值
   */
  bindKeyInput: function(e) {},

})